-
Notifications
You must be signed in to change notification settings - Fork 0
[DFS, BFS] 10월 11일 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
mingulmangul
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[DFS/BFS 구현 코드 리뷰 완료]
2615(P1)
안녕하세요 은솜님!
이번 구현 문제가 살짝 난이도가 있었는데 너무 잘 풀어주신 것 같습니다🥰
그리고 자바로 풀어주셔서 너무 반가웠습니다!! 저도 코테 언어를 자바로 하고 있어서요. 자바 질문 있으시면 언제든 편하게 물어봐주세요 ㅎㅎ
추가적으로 바꿔보면 좋을 것들 몇 가지 코멘트로 남겼습니다. 한 번 읽어봐주세요!
과제하느라 고생하셨습니다💚
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Scanner sc = new Scanner(System.in); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1. 자바에서는 입력에 BufferedReader, InputStreamReader를 사용하면 훨씬 빨라요! Scanner는 꽤 속도가 느린 편이라 잘 사용하지 않는다고 해요. 코드에 문제가 있는 건 아니지만 혹시 자바로 코딩테스트를 보실 예정이라면 꼭 알아두시면 좋을 라이브러리라 P1으로 코멘트 남겨드립니다 ㅎㅎ
| int[][] graph = new int[19][19]; | ||
| int[] dx = {0, 1, 1, -1}; | ||
| int[] dy = {1, 0, 1, 1}; | ||
|
|
||
| for (int i = 0; i < 19; i++) { | ||
| for (int j = 0; j < 19; j++) { | ||
| graph[i][j] = sc.nextInt(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2. 여기 반복해서 사용되는 숫자가 있네요! 자주 사용되는 숫자를 하드코딩하다 보면 실수할 가능성도 높아지기 때문에 따로 상수로 선언해준 뒤에 사용하는 것이 좋습니다 🤗 코드의 가독성이나 유지보수 측면에서도 좋아요!
| if (graph[i][j] != 0) { | ||
| int target = graph[i][j]; | ||
|
|
||
| for (int k = 0; k < 4; k++) { | ||
| int cnt = 1; | ||
| int nx = i + dx[k]; | ||
| int ny = j + dy[k]; | ||
|
|
||
| while (0 <= nx && nx < 19 && 0 <= ny && ny < 19 && graph[nx][ny] == target) { | ||
| cnt++; | ||
|
|
||
| if (cnt == 5) { | ||
| if (0 <= i - dx[k] && i - dx[k] < 19 && 0 <= j - dy[k] && j - dy[k] < 19 && | ||
| graph[i - dx[k]][j - dy[k]] == target) { | ||
| break; | ||
| } | ||
| if (0 <= nx + dx[k] && nx + dx[k] < 19 && 0 <= ny + dy[k] && ny + dy[k] < 19 && | ||
| graph[nx + dx[k]][ny + dy[k]] == target) { | ||
| break; | ||
| } | ||
|
|
||
| System.out.println(target); | ||
| System.out.println(i + 1 + " " + (j + 1)); | ||
| System.exit(0); | ||
| } | ||
|
|
||
| nx += dx[k]; | ||
| ny += dy[k]; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2. 이 부분 인덴테이션이 꽤 깊네요..! 인덴테이션은 보통 3 뎁스를 넘지 않도록 얕게 유지하는게 좋습니다. 맨 처음 if문을 조건에 맞지 않는 경우로 바꾸면 continue를 사용해서 바로 다음 반복으로 넘어갈 수 있어요! 이러면 현재 if문 안에 있는 코드들은 if문 밖에서 작성해도 되기 때문에 인덴테이션도 한 단계 줄일 수 있어요. 그리고 인덴테이션이 깊어지거나 한 메소드가 길어지면, 적절하게 기능을 나눠서 메소드로 분리하는게 좋습니다! 하나의 메소드는 하나의 역할만 수행하는게 객체지향설계의 핵심이에요
| while (0 <= nx && nx < 19 && 0 <= ny && ny < 19 && graph[nx][ny] == target) { | ||
| cnt++; | ||
|
|
||
| if (cnt == 5) { | ||
| if (0 <= i - dx[k] && i - dx[k] < 19 && 0 <= j - dy[k] && j - dy[k] < 19 && | ||
| graph[i - dx[k]][j - dy[k]] == target) { | ||
| break; | ||
| } | ||
| if (0 <= nx + dx[k] && nx + dx[k] < 19 && 0 <= ny + dy[k] && ny + dy[k] < 19 && | ||
| graph[nx + dx[k]][ny + dy[k]] == target) { | ||
| break; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2. 인덱스가 바둑판의 범위 내에 위치하는지, 그리고 해당 위치가 target과 같은 색인지 체크하는 조건이 계속 반복되네요! 반복되는 코드는 따로 메소드로 분리하면 코드의 의미도 더 명확해지고, 코드의 양이 줄어서 읽기도 더 쉬워질 것 같아요!
Goldchae
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[BFS&DFS 알고리즘 코드 리뷰 완료]
p3. 전체적으로 깔끔하고 좋은 코드로 작성해주셨네요! 여러 언어로 풀어주신 점 인상깊었습니다! 코멘트 남긴 부분 확인해주세요! 수고하셨습니다!
| vector<bool> visited; | ||
| int n; | ||
|
|
||
| int dfs(int start) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 문제는 BFS로는 쓸 수 없을까요? 가능하다면 한번 작성해보세요! DFS 함수도 재귀를 이용해서 구현해보셔도 도움이 많이 될 것 같습니다! 재귀가 많이 쓰인답니다.
| adj_list[b].push_back(a); // 역방향 그래프를 만듭니다. | ||
| } | ||
|
|
||
| vector<int> result(n + 1, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
n+1로 초기화해주시는 것 좋습니다!
| int[] dx = {1, -1, 0, 0, 1, -1, 1, -1}; | ||
| int[] dy = {0, 0, -1, 1, -1, 1, 1, -1}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8방향 탐색용 배열 두 개 선언 스킬 사용해주신 점 좋습니다!
| for (int i = 0; i < 8; i++) { | ||
| int nx = a + dx[i]; | ||
| int ny = b + dy[i]; | ||
| if (0 <= nx && nx < field.length && 0 <= ny && ny < field[0].length && field[nx][ny] == 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
범위 체크 부분이 길어질 떄(체크해야 할 범위가 많거나 변수가 너무 많을 떄) 해당 검사 체크 부분만 함수로 뺴서 다루셔도 됩니다!
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scanner보다 BufferedReader가 시간복잡도상 더 좋습니다!
###인적사항
학번 : 2271018
이름 : 김은솜
###과제제출
기존제출 : 1325, 19538, 2615, 4963, 게임맵최단거리
추가제출 :